//
//  ManagementViewController.m
//  HondaPro_2
//
//  Created by ht on 13-6-24.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "ManagementViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "NIDropDown.h"
#import "BookingDetailsViewController.h"
#import "UIViewController+MJPopupViewController.h"
#import "MyAFNetWorkManager.h"
#import "webConfig.h"
#import "webService.h"
#import "SVProgressHUD.h"
#import "MJRefresh.h"

@interface ManagementViewController ()
{
    int _isTheButtonTag;
    //控制表格行颜色
    BOOL _cellColor;
    
    NSString *btnType;//1是正常，2是取消进场，3是已来厂
    int pageIndex;
    int pageCount;
}

@end

@implementation ManagementViewController
//@synthesize secondDetailViewController = _secondDetailViewController;

@synthesize detailed1 = _detailed1;


@synthesize datePicker = _datePicker;

@synthesize thelabel1 = _thelabel1;
@synthesize thebutton1 = _thebutton1;
@synthesize thelabel2 = _thelabel2;
@synthesize thebutton2 = _thebutton2;
@synthesize thelabel3 = _thelabel3;
@synthesize thebutton3 = _thebutton3;
@synthesize thelabel4 = _thelabel4;
@synthesize thebutton4 = _thebutton4;

@synthesize dateString = _dateString;

@synthesize tableViewlabel1 = _tableViewlabel1;
@synthesize tableViewlabel2 = _tableViewlabel2;
@synthesize tableViewlabel3 = _tableViewlabel3;
@synthesize tableViewlabel4 = _tableViewlabel4;
@synthesize tableViewlabel5 = _tableViewlabel5;
@synthesize tableViewlabel6 = _tableViewlabel6;
@synthesize tableViewlabel7 = _tableViewlabel7;
@synthesize tableViewlabel8 = _tableViewlabel8;
@synthesize tableViewlabel10 = _tableViewlabel10;
@synthesize tableViewlabel11 = _tableViewlabel11;

@synthesize consultantlabel = _consultantlabel;
@synthesize registrationlabel = _registrationlabel;
@synthesize consultantbutton = _consultantbutton;
@synthesize registrationbutton = _registrationbutton;

@synthesize cardtextfield = _cardtextfield;

@synthesize phasebutton1 = _phasebutton1;
@synthesize phasebutton2 = _phasebutton2;
@synthesize phasebutton3 = _phasebutton3;

@synthesize tabelViewLabelArray = _tabelViewLabelArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
    
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self addNotification];
    pageIndex = 1;
    [self chare:nil];
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"房子.png"] forState:UIControlStateNormal];
    [backButton setFrame:CGRectMake(70, 42, 68, 40)];
    [backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    UILabel *jiedai = [[UILabel alloc]initWithFrame:CGRectMake(200, 45, 200, 40)];
    jiedai.text = @"预约查询";
    jiedai.backgroundColor = [UIColor clearColor];
    jiedai.textColor = [UIColor grayColor];
    jiedai.font = [UIFont systemFontOfSize:30];
    [self.view addSubview:jiedai];
    
    
    UIImageView *imageline = [[UIImageView alloc]initWithFrame:CGRectMake(68, 170, 885, 3)];
    imageline.image = [UIImage imageNamed:@"红色分割线.png"];
    [self.view addSubview:imageline];
    
    UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 1000, 11)];
    image1.image = [UIImage imageNamed:@"渐变分割线.png"];
    [self.view addSubview:image1];
    
    [self loadfacePictuer];
    
    [self loadLabelPicture];
    [self loadfieldPocture];
    
    //默认点击未进场
    [self phasebutton1:self.phasebutton1];
    
    
   
    
    self.tabelViewLabelArray1 = [[NSMutableArray alloc] init];
    self.tabelViewLabelArray2 = [[NSMutableArray alloc] init];
    self.tabelViewLabelArray3 = [[NSMutableArray alloc] init];
    
    self.detailed1 = [[UITableView alloc]initWithFrame:CGRectMake(78, 404, 865, 300)style:UITableViewStylePlain];
    [self.detailed1 setDelegate:self];
    [self.detailed1 setDataSource:self];
    [self.detailed1 setHidden:NO];
    [self.view addSubview:self.detailed1];
    
    __unsafe_unretained UITableView *table = self.detailed1;
    table.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        pageIndex = 1;
        [self chare:nil];
        [table.mj_header endRefreshing];
    }];
    
    
    table.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        pageIndex++;
        [self chare:nil];
        
        [table.mj_footer endRefreshing];
    }];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(comeFactory:) name:@"comeFactory" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(comeEdit:) name:@"comeEdit" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(toView:) name:@"toView" object:nil];

    self.saId = @"< 空 >";
    self.inputPersonId = @"< 空 >";
    self.saArray = [NSMutableArray arrayWithCapacity:5];
    self.inputPersonArray = [NSMutableArray arrayWithCapacity:5];
    
    _cellColor = YES;
}
//接收转来厂的消息
-(void)comeFactory:(NSNotification *)notification
{
    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
    
    NSDictionary *notiDic = notification.object;
    
    self.serviceHomeViewController = [[ServiceHomeViewController alloc]init];
    self.serviceHomeViewController.vin.text = [self.secondDetailViewController.detailsDictionary objectForKey:@"VIN"];
    self.serviceHomeViewController.plateNumber.text = [self.secondDetailViewController.detailsDictionary objectForKey:@"PLATE_NUMBER"];
    
    NSArray *modelArr = [self readershuDICimform][@"MODEL"];
    NSArray *seriesArr = [self readershuDICimform][@"SERIES"];
    NSArray *guwenArr = [self readershuDICimform][@"SERVICE_ADVISOR"];
    
    NSString *seriesStr;
    NSString *modelStr;
    NSString *guwenStr;
    
    NSMutableDictionary *dics = [NSMutableDictionary dictionaryWithCapacity:5];
    
    for (NSDictionary *dic in modelArr) {
        if ([dic[@"MODEL_CODE"] isEqualToString:notiDic[@"MODEL"]]) {
            modelStr = dic[@"MODEL_NAME"];
            [dics setValue:modelStr forKey:@"MODEL_NAME"];
            [dics setValue:dic[@"MODEL_CODE"] forKey:@"MODEL_CODE"];
        }
    }
    
    for (NSDictionary *dic in seriesArr) {
        if ([dic[@"SERIES_CODE"] isEqualToString:notiDic[@"SERIES"]]) {
            seriesStr = dic[@"SERIES_NAME"];
            
            [dics setValue:seriesStr forKey:@"SERIES_NAME"];
            [dics setValue:dic[@"SERIES_CODE"] forKey:@"SERIES_CODE"];
        }
    }
    
    for (NSDictionary *dic in guwenArr) {
        if ([dic[@"CODE"] isEqualToString:notiDic[@"SA_NAME"]]) {
            guwenStr = dic[@"NAME"];
            
            [dics setValue:guwenStr forKey:@"SA_NAME"];
            [dics setValue:dic[@"CODE"] forKey:@"SA_CODE"];
        }
    }
    
    [dics setValue:[self.secondDetailViewController.detailsDictionary objectForKey:@"CUSTOMER_NAME"] forKey:@"CUSTOMER_NAME"];
    self.serviceHomeViewController.turnServiceParam = dics;
    
    [self.navigationController pushViewController:self.serviceHomeViewController animated:YES];
}

- (void)comeEdit:(NSNotification *)noti
{
    self.xZengPopViewController = nil;
    self.xZengPopViewController = [[NewPopViewController alloc] init];
    self.xZengPopViewController.delegate = self;
    self.xZengPopViewController.editdDataDic = noti.object;
    
    [self presentView:self.xZengPopViewController.view completion:nil];
}

//查看派工界面
-(void)toView:(NSNotification *)notification
{
//    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
    ServiceAssignedViewController *savc = [[ServiceAssignedViewController alloc]init];
//    [self.navigationController pushViewController:savc animated:YES];
    [self presentViewController:savc animated:NO completion:nil];
    [savc modifyInstead];
}
-(void)refreshData:(NSNotification*)notification{
    
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    
    self.tabelViewLabelArray = Nil;
    self.tabelViewLabelArray = [theJson objectForKey:@"results"];
    
    if (self.currentItem==0) {
        [self phasebutton1:self.phasebutton1];
    }else if (self.currentItem==1) {
        [self phasebutton2:self.phasebutton2];
    }else{
        [self phasebutton3:self.phasebutton3];
    }
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//服务顾问
-(void)firstmenu:(id)sender
{
//    webService *webcon = [[webService alloc] init];
//    webcon.methodName = GETSANAMELIST;
//    [webcon commonGetSharedRequest:GETSANAMELIST wsparas:Nil];
    
    if (!contentCon) {
        contentCon=[[popContentViewController alloc] initWithNibName:@"popContentViewController" bundle:nil];
    }
    
    contentCon.delegate = self;
    contentCon.btnSender=sender;
    self.popCon = [[UIPopoverController alloc] initWithContentViewController:contentCon];
    [DDPopoverBackgroundView setTintColor:[UIColor colorWithRed:234 green:248 blue:242 alpha:0.9]];
    [DDPopoverBackgroundView setArrowBase:30.0];//箭头
    [DDPopoverBackgroundView setArrowHeight:15.0];
    [DDPopoverBackgroundView setContentInset:1.0];
    [self.popCon setPopoverBackgroundViewClass:[DDPopoverBackgroundView class]];
    [self.popCon setPopoverContentSize:CGSizeMake(self.consultantbutton.frame.size.width, 280)];
    [self.popCon presentPopoverFromRect:CGRectMake(100, 15, 120, 20) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
    NSArray *zidianArr=[self readershuDICimform][@"SERVICE_ADVISOR"];
    NSMutableArray *fuwuguwenArr = [NSMutableArray arrayWithCapacity:5];
    [self.saArray removeAllObjects];
    [self.saArray setArray:zidianArr];
    [self.saArray insertObject:@"< 空 >" atIndex:0];
    
    for(NSDictionary *dic in zidianArr)
    {
        [fuwuguwenArr addObject:dic[@"NAME"]];
        //[self.saArray addObject:dic[@"CODE"]];
    }
    
    [fuwuguwenArr insertObject:@"< 空 >" atIndex:0];
    contentCon.list = fuwuguwenArr;
    [contentCon.myTableView reloadData];
    
}

-(void)getSaName:(NSNotification*)notification
{
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    self.saArray = [theJson objectForKey:@"results"];
    NSMutableArray *carTypeArray=[[NSMutableArray alloc] init];
    [carTypeArray addObject:@"< 空 >"];
    
    for (int i=0; i<[[theJson objectForKey:@"results"] count]; i++) {
        
        [carTypeArray addObject: [[[theJson objectForKey:@"results"] objectAtIndex:i] objectForKey:@"saName"]];
    }
    
    contentCon.list = carTypeArray;
    
    [contentCon.myTableView reloadData];
    
}
//弹出回调方法
-(void)selectDelegate:(NSInteger)row btn:(UIButton *)btn{
    if (row>0) {
        
        if (btn==self.consultantbutton) {
           
            self.saId = [[self.saArray objectAtIndex:row] objectForKey:@"CODE"];
        }else if (btn==self.registrationbutton){
            self.inputPersonId = [[self.inputPersonArray objectAtIndex:row] objectForKey:@"CODE"];
        }
        
    }else{
        if (btn==self.consultantbutton) {
            self.saId = @"< 空 >";
        }else if (btn==self.registrationbutton){
            self.inputPersonId = @"< 空 >";
        }
        
    }
    
    [self.popCon dismissPopoverAnimated:YES];
    self.popCon = nil;
}
//维修分类
-(void)secondmenu:(id)sender
{
    if (!contentCon) {
        contentCon=[[popContentViewController alloc] initWithNibName:@"popContentViewController" bundle:nil];
    }
    
    contentCon.delegate = self;
    contentCon.btnSender=sender;
    self.popCon = [[UIPopoverController alloc] initWithContentViewController:contentCon];
    [DDPopoverBackgroundView setTintColor:[UIColor colorWithRed:234 green:248 blue:242 alpha:0.9]];
    [DDPopoverBackgroundView setArrowBase:30.0];//箭头
    [DDPopoverBackgroundView setArrowHeight:15.0];
    [DDPopoverBackgroundView setContentInset:1.0];
    [self.popCon setPopoverBackgroundViewClass:[DDPopoverBackgroundView class]];
    [self.popCon setPopoverContentSize:CGSizeMake(self.registrationbutton.frame.size.width, 280)];
    [self.popCon presentPopoverFromRect:CGRectMake(100, 15, 120, 20) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    NSArray *zidianArr=[self readershuDICimform][@"TM_REPAIR_TYPE_CODE"];
    
    NSMutableArray *fuwuguwenArr = [NSMutableArray arrayWithCapacity:5];
    [self.inputPersonArray removeAllObjects];
    [self.inputPersonArray setArray:zidianArr];
    [self.inputPersonArray insertObject:@"< 空 >" atIndex:0];
    
    for(NSDictionary *dic in zidianArr)
    {
        [fuwuguwenArr addObject:dic[@"NAME"]];
        //[self.saArray addObject:dic[@"CODE"]];
    }
    
    [fuwuguwenArr insertObject:@"< 空 >" atIndex:0];
    contentCon.list = fuwuguwenArr;
    [contentCon.myTableView reloadData];
}

-(void)getInputPersonName:(NSNotification*)notification{

    
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
    self.inputPersonArray = [theJson objectForKey:@"results"];
    NSMutableArray *carTypeArray=[[NSMutableArray alloc] init];
    [carTypeArray addObject:@"< 空 >"];
    
    for (int i=0; i<[[theJson objectForKey:@"results"] count]; i++) {
        
        [carTypeArray addObject: [[[theJson objectForKey:@"results"] objectAtIndex:i] objectForKey:@"inputPersonName"]];
    }
    
    contentCon.list = carTypeArray;
    
    [contentCon.myTableView reloadData];
    
}

-(void)cancel:(id)sender
{
    [self.datePicker setHidden:YES];
}

#define mark --时间选择期
-(void)thebutton1:(id)sender
{
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 280, 160)];
    self.datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:NSLocalizedString(@"Chinese", @"")];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    [self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    //
    panleCon = [[UIViewController alloc] init];
    [panleCon.view addSubview:self.datePicker];
    self.popCon = [[UIPopoverController alloc] initWithContentViewController:panleCon];
    self.popCon.popoverContentSize = CGSizeMake(280.0f, 200.0f);
    
    [self.popCon presentPopoverFromRect:CGRectMake(20, 0, 10, 40) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    UIButton *pickerBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pickerBut setBackgroundColor:[UIColor clearColor]];
    [pickerBut setTitle:@"今 天" forState:UIControlStateNormal];
    [pickerBut setFrame:CGRectMake(20, 165, 80, 30)];
    [pickerBut addTarget:self action:@selector(pickerBut:) forControlEvents:UIControlEventTouchUpInside];
    [panleCon.view addSubview:pickerBut];
    UIButton *pickerBut1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pickerBut1 setBackgroundColor:[UIColor clearColor]];
    [pickerBut1 setTitle:@"清 除" forState:UIControlStateNormal];
    [pickerBut1 setFrame:CGRectMake(180, 165, 80, 30)];
    [pickerBut1 addTarget:self action:@selector(pickerBut1:) forControlEvents:UIControlEventTouchUpInside];
    [panleCon.view addSubview:pickerBut1];
}
-(void)dateChanged:(id)sender{
    UIDatePicker *contro = (UIDatePicker*)sender;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    self.dateString = [formatter stringFromDate:contro.date];
    [self.thebutton1 setTitle:self.dateString forState:UIControlStateNormal];
}
-(void)pickerBut:(id)sender
{
    NSDate *senddate=[NSDate date];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    self.dateString = [dateformatter stringFromDate:senddate];
    [self.thebutton1 setTitle:self.dateString forState:UIControlStateNormal];
    [self.popCon dismissPopoverAnimated:YES];
}
-(void)pickerBut1:(id)sender
{
    [self.thebutton1 setTitle:@"请选择" forState:UIControlStateNormal];
    [self.popCon dismissPopoverAnimated:YES];
}
-(void)thebutton2:(id)sender
{    
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 280, 160)];
    self.datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:NSLocalizedString(@"Chinese", @"")];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    
    [self.datePicker addTarget:self action:@selector(dateChanged2:) forControlEvents:UIControlEventValueChanged];
    //
    panleCon = [[UIViewController alloc] init];
    [panleCon.view addSubview:self.datePicker];
    self.popCon = [[UIPopoverController alloc] initWithContentViewController:panleCon];
    self.popCon.popoverContentSize = CGSizeMake(280.0f, 200.0f);
    
    [self.popCon presentPopoverFromRect:CGRectMake(20, 0, 10, 40) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
    UIButton *pickerBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pickerBut setBackgroundColor:[UIColor clearColor]];
    [pickerBut setTitle:@"今 天" forState:UIControlStateNormal];
    [pickerBut setFrame:CGRectMake(20, 165, 80, 30)];
    [pickerBut addTarget:self action:@selector(pickerBut2:) forControlEvents:UIControlEventTouchUpInside];
    [panleCon.view addSubview:pickerBut];
    UIButton *pickerBut1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pickerBut1 setBackgroundColor:[UIColor clearColor]];
    [pickerBut1 setTitle:@"清 除" forState:UIControlStateNormal];
    [pickerBut1 setFrame:CGRectMake(180, 165, 80, 30)];
    [pickerBut1 addTarget:self action:@selector(pickerBut3:) forControlEvents:UIControlEventTouchUpInside];
    [panleCon.view addSubview:pickerBut1];
}
-(void)dateChanged2:(id)sender{
    UIDatePicker *contro = (UIDatePicker*)sender;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    self.dateString = [formatter stringFromDate:contro.date];
    [self.thebutton2 setTitle:self.dateString forState:UIControlStateNormal];
}
-(void)pickerBut2:(id)sender
{
    NSDate *senddate=[NSDate date];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    self.dateString = [dateformatter stringFromDate:senddate];
    [self.thebutton2 setTitle:self.dateString forState:UIControlStateNormal];
    [self.popCon dismissPopoverAnimated:YES];
}
-(void)pickerBut3:(id)sender
{
    [self.thebutton2 setTitle:@"请选择" forState:UIControlStateNormal];
    [self.popCon dismissPopoverAnimated:YES];
}
-(void)thebutton3:(id)sender
{
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 280, 160)];
    self.datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:NSLocalizedString(@"Chinese", @"")];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    
    [self.datePicker addTarget:self action:@selector(dateChanged3:) forControlEvents:UIControlEventValueChanged];
    //
    panleCon = [[UIViewController alloc] init];
    [panleCon.view addSubview:self.datePicker];
    self.popCon = [[UIPopoverController alloc] initWithContentViewController:panleCon];
    self.popCon.popoverContentSize = CGSizeMake(280.0f, 200.0f);
    
    [self.popCon presentPopoverFromRect:CGRectMake(20, 0, 10, 40) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    UIButton *pickerBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pickerBut setBackgroundColor:[UIColor clearColor]];
    [pickerBut setTitle:@"今 天" forState:UIControlStateNormal];
    [pickerBut setFrame:CGRectMake(20, 165, 80, 30)];
    [pickerBut addTarget:self action:@selector(pickerBut4:) forControlEvents:UIControlEventTouchUpInside];
    [panleCon.view addSubview:pickerBut];
    UIButton *pickerBut1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pickerBut1 setBackgroundColor:[UIColor clearColor]];
    [pickerBut1 setTitle:@"清 除" forState:UIControlStateNormal];
    [pickerBut1 setFrame:CGRectMake(180, 165, 80, 30)];
    [pickerBut1 addTarget:self action:@selector(pickerBut5:) forControlEvents:UIControlEventTouchUpInside];
    [panleCon.view addSubview:pickerBut1];

}
-(void)dateChanged3:(id)sender{
    UIDatePicker *contro = (UIDatePicker*)sender;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    self.dateString = [formatter stringFromDate:contro.date];
    [self.thebutton3 setTitle:self.dateString forState:UIControlStateNormal];
}
-(void)pickerBut4:(id)sender
{
    NSDate *senddate=[NSDate date];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    self.dateString = [dateformatter stringFromDate:senddate];
    [self.thebutton3 setTitle:self.dateString forState:UIControlStateNormal];
    [self.popCon dismissPopoverAnimated:YES];
}
-(void)pickerBut5:(id)sender
{
    [self.thebutton3 setTitle:@"请选择" forState:UIControlStateNormal];
    [self.popCon dismissPopoverAnimated:YES];
}

-(void)thebutton4:(id)sender
{
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 280, 160)];
    self.datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:NSLocalizedString(@"Chinese", @"")];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    
    [self.datePicker addTarget:self action:@selector(dateChanged4:) forControlEvents:UIControlEventValueChanged];
    //
    panleCon = [[UIViewController alloc] init];
    [panleCon.view addSubview:self.datePicker];
    self.popCon = [[UIPopoverController alloc] initWithContentViewController:panleCon];
    self.popCon.popoverContentSize = CGSizeMake(280.0f, 200.0f);
    
    [self.popCon presentPopoverFromRect:CGRectMake(20, 0, 10, 40) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    UIButton *pickerBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pickerBut setBackgroundColor:[UIColor clearColor]];
    [pickerBut setTitle:@"今 天" forState:UIControlStateNormal];
    [pickerBut setFrame:CGRectMake(20, 165, 80, 30)];
    [pickerBut addTarget:self action:@selector(pickerBut6:) forControlEvents:UIControlEventTouchUpInside];
    [panleCon.view addSubview:pickerBut];
    UIButton *pickerBut1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [pickerBut1 setBackgroundColor:[UIColor clearColor]];
    [pickerBut1 setTitle:@"清 除" forState:UIControlStateNormal];
    [pickerBut1 setFrame:CGRectMake(180, 165, 80, 30)];
    [pickerBut1 addTarget:self action:@selector(pickerBut7:) forControlEvents:UIControlEventTouchUpInside];
    [panleCon.view addSubview:pickerBut1];
}
-(void)dateChanged4:(id)sender{
    UIDatePicker *contro = (UIDatePicker*)sender;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    self.dateString = [formatter stringFromDate:contro.date];
    [self.thebutton4 setTitle:self.dateString forState:UIControlStateNormal];
}
-(void)pickerBut6:(id)sender
{
    NSDate *senddate=[NSDate date];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    self.dateString = [dateformatter stringFromDate:senddate];
    [self.thebutton4 setTitle:self.dateString forState:UIControlStateNormal];
    [self.popCon dismissPopoverAnimated:YES];
}
-(void)pickerBut7:(id)sender
{
    [self.thebutton4 setTitle:@"请选择" forState:UIControlStateNormal];
    [self.popCon dismissPopoverAnimated:YES];
}

#pragma mark - 绘制tableVeiw表头
-(void)loadLabelPicture
{
    
    //@"来源" ,@"  登记时间"
    NSMutableArray *labelMutabelArray = [[NSMutableArray alloc]initWithObjects:@" 预约号",@"车牌号",@"维修分类",@"客户名称",@"电话",@"预约时间",@"服务顾问", nil];
    UIView *colorLabel = [[UIView alloc]initWithFrame:CGRectMake(78, 360, 865, 45)];
    colorLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [self.view addSubview:colorLabel];
    CGFloat labelW=colorLabel.mj_w/labelMutabelArray.count;
    for(int i = 0; i <labelMutabelArray.count;i++)
    {
        UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(i*labelW, 0, labelW, colorLabel.mj_h)];
        titlelabel.backgroundColor = [UIColor clearColor];
        titlelabel.text = [labelMutabelArray objectAtIndex:i];
        titlelabel.textColor = [UIColor grayColor];
        titlelabel.font =HJFont(16);
        [titlelabel setTextAlignment:NSTextAlignmentCenter];
        [colorLabel addSubview:titlelabel];
    }
}

#pragma mark - tabaViewDelegate方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(_cellColor == YES)
    {
        return 6;
    }
    if(_cellColor == NO)
    {
        if (self.currentItem==0) {
            return [self.tabelViewLabelArray1 count];
        }else if (self.currentItem==1){
            return [self.tabelViewLabelArray2 count];
        }else{
            return [self.tabelViewLabelArray3 count];
        }
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indetfire = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetfire];
    if(nil == cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetfire];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    for(UIView *view in cell.contentView.subviews)
    {
        [view removeFromSuperview];
    }
    [self loadTableViewCellLabel];
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    if (self.currentItem==0) {
        array =[NSMutableArray arrayWithArray:self.tabelViewLabelArray1];
    }else if (self.currentItem==1){
        array =[NSMutableArray arrayWithArray:self.tabelViewLabelArray2];
    }else{
        array =[NSMutableArray arrayWithArray:self.tabelViewLabelArray3];
    }
    

    if(_cellColor == NO)
    {
        [cell.contentView addSubview:self.tableViewlabel1];
        [self.tableViewlabel2 setText:[[array objectAtIndex:[indexPath row]] objectForKey:@"PRECONTRACT_ID"]];
        [cell.contentView addSubview:self.tableViewlabel2];
        
        [self.tableViewlabel3 setText:[[array objectAtIndex:[indexPath row]] objectForKey:@"PLATE_NUMBER"]];
        [cell.contentView addSubview:self.tableViewlabel3];
        
        NSString*repairType=[[array objectAtIndex:[indexPath row]] objectForKey:@"REPAIR_TYPE"];
        [self.tableViewlabel4 setText:[NSString stringWithFormat:@"%@",repairType?repairType:@""]];
        [cell.contentView addSubview:self.tableViewlabel4];
        [self.tableViewlabel5 setText:[[array objectAtIndex:[indexPath row]] objectForKey:@"CUSTOMER_NAME"]];
        [cell.contentView addSubview:self.tableViewlabel5];
        [self.tableViewlabel6 setText:[[array objectAtIndex:[indexPath row]] objectForKey:@"level"]];
        [cell.contentView addSubview:self.tableViewlabel6];
        
        //NSNumber *tel = [[array objectAtIndex:[indexPath row]] objectForKey:@"MOBILE"];
        [self.tableViewlabel7 setText:[[array objectAtIndex:[indexPath row]] objectForKey:@"MOBILE"]];
        
        
        [cell.contentView addSubview:self.tableViewlabel7];
        [self.tableViewlabel8 setText:[[array objectAtIndex:[indexPath row]] objectForKey:@"START_TIME"]];
        [cell.contentView addSubview:self.tableViewlabel8];
        [self.tableViewlabel9 setText:[[array objectAtIndex:[indexPath row]] objectForKey:@"END_TIME"]];
        [cell.contentView addSubview:self.tableViewlabel9];
        
        NSString *fuwuguwen = [[array objectAtIndex:[indexPath row]] objectForKey:@"SA_NAME"];
        NSArray *zidianArr1 = [self readershuDICimform][@"SERVICE_ADVISOR"];
        for(NSDictionary *dic in zidianArr1)
        {
            if ([dic[@"CODE"] isEqualToString:fuwuguwen]) {
                [self.tableViewlabel10 setText:dic[@"NAME"]];
            }
        }
        [cell.contentView addSubview:self.tableViewlabel10];
    }
   
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == self.detailed1)
    {
        if(indexPath.row%2 == 1)
        {
            cell.backgroundColor = [UIColor whiteColor];
        }
        else
        {
            cell.backgroundColor = [[UIColor alloc]initWithRed:226.00/255 green:226.00/255 blue:226.00/255 alpha:1];
        }
        
    }   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *detailDic;
    if (self.currentItem==0) {
        detailDic = [self.tabelViewLabelArray1 objectAtIndex:[indexPath row]];
    }if (self.currentItem==1) {
        detailDic = [self.tabelViewLabelArray2 objectAtIndex:[indexPath row]];
    }if (self.currentItem==2) {
        detailDic = [self.tabelViewLabelArray3 objectAtIndex:[indexPath row]];
    }
    
    self.secondDetailViewController = [[BookingDetailsViewController alloc] init];
    self.secondDetailViewController.detailsDictionary = [detailDic mutableCopy];
    if (self.currentItem==2) {
        self.secondDetailViewController.isShow=NO;
    }
    
    [self.secondDetailViewController loadBitchLabel];
    [self.secondDetailViewController loadtitleLabel];
    //[self presentPopupViewController:self.secondDetailViewController animationType:MJPopupViewAnimationFade];
    [self presentView:self.secondDetailViewController.view completion:nil];
}

-(void)xugaisucess{
    pageIndex = 1;
    [self chare:self.queryBtn];
}
-(void)getprecontractDetail:(NSNotification*)notification
{
    NSMutableDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
 
    self.secondDetailViewController = [[BookingDetailsViewController alloc] init];
    self.secondDetailViewController.detailsDictionary = theJson;
    if (self.currentItem==2) {
        self.secondDetailViewController.isShow=NO;
    }
    
    [self presentPopupViewController:self.secondDetailViewController animationType:MJPopupViewAnimationFade];
}

-(void)loadfieldPocture
{
//    self.thelabel1 = [[UILabel alloc]initWithFrame:CGRectMake(160, 195, 132, 35)];
//    [self.thelabel1 setBackgroundColor:[UIColor whiteColor]];
//    self.thelabel1.layer.borderColor = [[UIColor grayColor]CGColor];
//    self.thelabel1.layer.borderWidth = 0.8;
//    [self.view addSubview:self.thelabel1];
//    self.thebutton1 = [[UIButton alloc]initWithFrame:CGRectMake(161, 196, 130, 33)];
//    [self.thebutton1 setBackgroundImage:[UIImage imageNamed:@"下拉框.png"] forState:UIControlStateNormal];
//    [self.thebutton1 addTarget:self action:@selector(thebutton1:) forControlEvents:UIControlEventTouchUpInside];
//    [self.thebutton1 setTitle:@"请选择" forState:UIControlStateNormal];
//    self.thebutton1.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:16];
//    [self.thebutton1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    [self.view addSubview:self.thebutton1];
//
//    self.thelabel2 = [[UILabel alloc]initWithFrame:CGRectMake(335, 195, 132, 35)];
//    [self.thelabel2 setBackgroundColor:[UIColor whiteColor]];
//    self.thelabel2.layer.borderColor = [[UIColor grayColor]CGColor];
//    self.thelabel2.layer.borderWidth = 0.8;
//    [self.view addSubview:self.thelabel2];
//    self.thebutton2 = [[UIButton alloc]initWithFrame:CGRectMake(336, 196, 130, 33)];
//    [self.thebutton2 setBackgroundImage:[UIImage imageNamed:@"下拉框.png"] forState:UIControlStateNormal];
//    [self.thebutton2 addTarget:self action:@selector(thebutton2:) forControlEvents:UIControlEventTouchUpInside];
//    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//    [formatter setDateFormat:@"yyyy-MM-dd"];
//    [self.thebutton2 setTitle:@"请选择" forState:UIControlStateNormal];
//    self.thebutton2.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:16];
//    [self.thebutton2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    [self.view addSubview:self.thebutton2];
    
    
    //
    //CGRectMake(600, 195, 132, 35)
    self.thelabel3 = [[UILabel alloc]initWithFrame:CGRectMake(160, 195, 132, 35)];
    [self.thelabel3 setBackgroundColor:[UIColor whiteColor]];
    self.thelabel3.layer.borderColor = [[UIColor grayColor]CGColor];
    self.thelabel3.layer.borderWidth = 0.8;
    [self.view addSubview:self.thelabel3];
    //CGRectMake(601, 196, 130, 33)
    self.thebutton3 = [[UIButton alloc]initWithFrame:CGRectMake(161, 196, 130, 33)];
    [self.thebutton3 setBackgroundImage:[UIImage imageNamed:@"下拉框.png"] forState:UIControlStateNormal];
    [self.thebutton3 addTarget:self action:@selector(thebutton3:) forControlEvents:UIControlEventTouchUpInside];
    [self.thebutton3 setTitle:@"请选择" forState:UIControlStateNormal];
    self.thebutton3.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:16];
    [self.thebutton3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.view addSubview:self.thebutton3];
    
    
    
    
    //
    //CGRectMake(770, 195, 135, 35)
    self.thelabel4 = [[UILabel alloc]initWithFrame:CGRectMake(335, 195, 132, 35)];
    [self.thelabel4 setBackgroundColor:[UIColor whiteColor]];
    self.thelabel4.layer.borderColor = [[UIColor grayColor]CGColor];
    self.thelabel4.layer.borderWidth = 0.8;
    [self.view addSubview:self.thelabel4];
    
    //
    //CGRectMake(771, 196, 133, 33)
    self.thebutton4 = [[UIButton alloc]initWithFrame:CGRectMake(336, 196, 130, 33)];
    [self.thebutton4 setBackgroundImage:[UIImage imageNamed:@"下拉框.png"] forState:UIControlStateNormal];
    [self.thebutton4 addTarget:self action:@selector(thebutton4:) forControlEvents:UIControlEventTouchUpInside];
    [self.thebutton4 setTitle:@"请选择" forState:UIControlStateNormal];
    self.thebutton4.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:16];
    [self.thebutton4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.view addSubview:self.thebutton4];
    
    //160.311
    self.cardtextfield = [[UITextField alloc]initWithFrame:CGRectMake(600, 250, 307, 34)];
    self.cardtextfield.borderStyle = UITextBorderStyleLine;
    self.cardtextfield.textColor = [UIColor grayColor];
    self.cardtextfield.font = [UIFont fontWithName:@"ArialMT" size:15];
    [self.cardtextfield setTextAlignment:NSTextAlignmentCenter];
    self.cardtextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.cardtextfield.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
    
    //初始化车牌号
    self.cardtextfield.text = [MainMenuDataModel sharedMainMenuDataModel].strCurrentPlateNumber == nil ? @"" : [MainMenuDataModel sharedMainMenuDataModel].strCurrentPlateNumber;
    
    [self.view addSubview:self.cardtextfield];
    
    //160 250
    self.consultantlabel = [[UILabel alloc]initWithFrame:CGRectMake(600, 195, 307, 35)];
    [self.consultantlabel setBackgroundColor:[UIColor whiteColor]];
    self.consultantlabel.layer.borderColor = [[UIColor grayColor]CGColor];
    self.consultantlabel.layer.borderWidth = 0.8;
    [self.view addSubview:self.consultantlabel];
    self.consultantbutton = [[UIButton alloc]initWithFrame:CGRectMake(597, 192, 313, 40)];
    [self.consultantbutton setBackgroundImage:[UIImage imageNamed:@"10.png"] forState:UIControlStateNormal];
    [self.consultantbutton addTarget:self action:@selector(firstmenu:) forControlEvents:UIControlEventTouchUpInside];
    [self.consultantbutton setTitle:@"< 空 >" forState:UIControlStateNormal];
    self.consultantbutton.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:16];
    [self.consultantbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.view addSubview:self.consultantbutton];
    
    //600, 240。维修分类
    self.registrationlabel = [[UILabel alloc]initWithFrame:CGRectMake(160,250, 307, 35)];
    [self.registrationlabel setBackgroundColor:[UIColor whiteColor]];
    self.registrationlabel.layer.borderColor = [[UIColor grayColor]CGColor];
    self.registrationlabel.layer.borderWidth = 0.8;
    [self.view addSubview:self.registrationlabel];
    self.registrationbutton = [[UIButton alloc]initWithFrame:CGRectMake(157, 247, 313, 40)];
    [self.registrationbutton setBackgroundImage:[UIImage imageNamed:@"10.png"] forState:UIControlStateNormal];
    [self.registrationbutton setTitle:@"< 空 >" forState:UIControlStateNormal];
    self.registrationbutton.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:16];
    [self.registrationbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.registrationbutton addTarget:self action:@selector(secondmenu:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registrationbutton];
    
    //预约
    self.phasebutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.phasebutton1 setFrame:CGRectMake(75, 125, 100, 45)];
    [self.phasebutton1 setTitle:@"未进场" forState:UIControlStateNormal];
    [self.phasebutton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.phasebutton1 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ht05-1.png"]]];
    [self.phasebutton1 addTarget:self action:@selector(phasebutton1:) forControlEvents:UIControlEventTouchUpInside];
    [self.phasebutton1 setTag:10];
    [self.view addSubview:self.phasebutton1];
    
    self.phasebutton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.phasebutton2 setFrame:CGRectMake(180, 125, 100, 45)];
    [self.phasebutton2 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
    [self.phasebutton2 setTitle:@"取消进场" forState:UIControlStateNormal];
    [self.phasebutton2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.phasebutton2 addTarget:self action:@selector(phasebutton2:) forControlEvents:UIControlEventTouchUpInside];
    [self.phasebutton2 setTag:20];
    [self.view addSubview:self.phasebutton2];
    
    self.phasebutton3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.phasebutton3 setFrame:CGRectMake(285, 125, 100, 45)];
    [self.phasebutton3 setTitle:@"已来厂" forState:UIControlStateNormal];
    [self.phasebutton3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.phasebutton3 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
    [self.phasebutton3 addTarget:self action:@selector(phasebutton3:) forControlEvents:UIControlEventTouchUpInside];
    [self.phasebutton3 setTag:30];
    [self.view addSubview:self.phasebutton3];
    
    self.phasebutton4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.phasebutton4 setFrame:CGRectMake(390, 125, 100, 45)];
    [self.phasebutton4 setTitle:@"已作单" forState:UIControlStateNormal];
    [self.phasebutton4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.phasebutton4 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
    [self.phasebutton4 addTarget:self action:@selector(phasebutton4:) forControlEvents:UIControlEventTouchUpInside];
    [self.phasebutton4 setTag:40];
    //[self.view addSubview:self.phasebutton4];

    
}
//已做单
-(void)phasebutton4:(id)sender
{
    if(self.phasebutton4.tag == 40)
    {
        [self.phasebutton1 setTitle:@"未进场" forState:UIControlStateNormal];
        [self pickerBut5:nil];
        [self.phasebutton1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton1 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
        
        [self.phasebutton2 setTitle:@"取消进场" forState:UIControlStateNormal];
        [self.phasebutton2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton2 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
        
        [self.phasebutton3 setTitle:@"已来厂" forState:UIControlStateNormal];
        [self.phasebutton3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [self.phasebutton3 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];

        [self.phasebutton4 setTitle:@"已作单" forState:UIControlStateNormal];
        [self.phasebutton4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton4 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ht05-1.png"]]];
    }
    self.currentItem = 0;
    
    for (int i=0; i<self.tabelViewLabelArray.count; i++) {
        
        if ([[[self.tabelViewLabelArray objectAtIndex:i] objectForKey:@"precontractStatus"] isEqual:@"已作单"])
        {
            [self.tabelViewLabelArray1 addObject:[self.tabelViewLabelArray objectAtIndex:i]];
            
        }
    }
    [self.detailed1 reloadData];
    [SVProgressHUD dismiss];
}


//正常预约
-(void)phasebutton1:(id)sender
{
    if(self.phasebutton1.tag == 10)
    {
        [self pickerBut4:self.thebutton3];
        [self.phasebutton1 setTitle:@"未进场" forState:UIControlStateNormal];
        [self.phasebutton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.phasebutton1 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ht05-1.png"]]];
        
        [self.phasebutton2 setTitle:@"取消进场" forState:UIControlStateNormal];
        [self.phasebutton2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton2 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
        
        [self.phasebutton3 setTitle:@"已来厂" forState:UIControlStateNormal];
        [self.phasebutton3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
      
        [self.phasebutton3 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
        [self.phasebutton4 setTitle:@"已作单" forState:UIControlStateNormal];
        [self.phasebutton4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton4 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
    }
    self.currentItem = 0;
    btnType = @"1";
    pageIndex = 1;
    [self chare:nil];
    
}

//过期预约
-(void)phasebutton2:(id)sender
{
    if(self.phasebutton2.tag == 20)
    {
        [self.phasebutton2 setTitle:@"取消进场" forState:UIControlStateNormal];
        [self pickerBut5:nil];
        [self.phasebutton2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.phasebutton2 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ht05-1.png"]]];
        
        [self.phasebutton1 setTitle:@"未进场" forState:UIControlStateNormal];
        [self.phasebutton1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton1 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
        
        [self.phasebutton3 setTitle:@"已来厂" forState:UIControlStateNormal];
        [self.phasebutton3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton3 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
        
        [self.phasebutton4 setTitle:@"已作单" forState:UIControlStateNormal];
        [self.phasebutton4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton4 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
    }
    
    self.currentItem = 1;
    btnType = @"2";
    pageIndex = 1;
    [self chare:nil];
}

//预约已来厂
-(void)phasebutton3:(id)sender
{
    if(self.phasebutton3.tag == 30)
    {
        [self.phasebutton3 setTitle:@"已来厂" forState:UIControlStateNormal];
        [self pickerBut5:nil];
        [self.phasebutton3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.phasebutton3 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ht05-1.png"]]];
        
        [self.phasebutton1 setTitle:@"未进场" forState:UIControlStateNormal];
        [self.phasebutton1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton1 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
        
        [self.phasebutton2 setTitle:@"取消进场" forState:UIControlStateNormal];
        [self.phasebutton2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton2 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
        
        [self.phasebutton4 setTitle:@"已作单" forState:UIControlStateNormal];
        [self.phasebutton4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.phasebutton4 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"改-灰but.png"]]];
    }
    
    self.currentItem = 2;
    btnType = @"3";
    pageIndex = 1;
    [self chare:nil];
}

-(void)loadfacePictuer
{
//    UILabel *record = [[UILabel alloc]initWithFrame:CGRectMake(80, 190, 100, 40)];
//    record.text = @"登记时间:";
//    record.backgroundColor = [UIColor clearColor];
//    record.textColor = [UIColor grayColor];
//    record.font = [UIFont fontWithName:@"ArialMT" size:17];
//    [self.view addSubview:record];
    
    //
    //CGRectMake(80, 310, 100, 40)
    UILabel *arrival = [[UILabel alloc]initWithFrame:CGRectMake(510, 252, 100, 40)];
    arrival.text = @"车  牌  号:";
    arrival.backgroundColor = [UIColor clearColor];
    arrival.textColor = [UIColor grayColor];
    arrival.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:arrival];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(305, 190, 40, 40)];
    label1.text = @"至";
    label1.backgroundColor = [UIColor clearColor];
    label1.textColor = [UIColor grayColor];
    label1.font = [UIFont fontWithName:@"ArialMT" size:17];
    [self.view addSubview:label1];
    
//    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(740, 190, 40, 40)];
//    label2.text = @"至";
//    label2.backgroundColor = [UIColor clearColor];
//    label2.textColor = [UIColor grayColor];
//    label2.font = [UIFont fontWithName:@"ArialMT" size:17];
//    [self.view addSubview:label2];
    
    //
    UILabel *brand = [[UILabel alloc]initWithFrame:CGRectMake(80, 190, 100, 40)];
    brand.text = @"预约时间:";
    brand.backgroundColor = [UIColor clearColor];
    brand.textColor = [UIColor grayColor];
    brand.font = [UIFont fontWithName:@"ArialMT" size:17];
    [self.view addSubview:brand];
    
    //
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(510, 190, 100, 40)];
    label3.text = @"服务顾问:";
    label3.backgroundColor = [UIColor clearColor];
    label3.textColor = [UIColor grayColor];
    label3.font = [UIFont fontWithName:@"ArialMT" size:17];
    [self.view addSubview:label3];
    
    UILabel *consultant = [[UILabel alloc]initWithFrame:CGRectMake(80, 252, 100, 40)];
    consultant.text = @"维修分类:";
    consultant.backgroundColor = [UIColor clearColor];
    consultant.textColor = [UIColor grayColor];
    consultant.font = [UIFont fontWithName:@"ArialMT" size:17];
    [self.view addSubview:consultant];
    //查询和新增按钮
    self.queryBtn = [[UIButton alloc]initWithFrame:CGRectMake(855, 50, 75, 33)];
    [self.queryBtn setBackgroundImage:[UIImage imageNamed:@"ht05-1.png"] forState:UIControlStateNormal];
    [self.queryBtn setTitle:@"查 询" forState:UIControlStateNormal];
    [self.queryBtn setTintColor:[UIColor whiteColor]];
    [self.queryBtn addTarget:self action:@selector(chare:) forControlEvents:UIControlEventTouchUpInside];
    [self.queryBtn setTag:20];
    [self.view addSubview:self.queryBtn];
    
    self.theNewBtn = [[UIButton alloc]initWithFrame:CGRectMake(855, 315, 75, 35)];
    [self.theNewBtn setTitle:@"新  增" forState:UIControlStateNormal];
    [self.theNewBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.theNewBtn setBackgroundImage:[UIImage imageNamed:@"改-灰but.png"] forState:UIControlStateNormal];
    [self.theNewBtn addTarget:self action:@selector(theNewBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.theNewBtn.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:16];
    [self.view addSubview:self.theNewBtn];
}

#pragma mark -返回的回调
-(void)back:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    [self removeNotification];
}

//查询按钮方法
-(void)chare:(id)sender
{
    _cellColor = NO;
    
    [self getSearchParameters];
    [SVProgressHUD showWithStatus:@"请等待...."];
    
    [MyAFNetWorkManager postWithParam:self.queryDic andAction:@"QUERY_BOOKING_ORDER_PAD" finish:^(NSData *data, NSDictionary *obj, NSError *error)
     {
         
         [SVProgressHUD dismiss];
         if (error == nil)
         {
             NSDictionary  *returnDic=(NSDictionary *)obj;
             NSLog(@"预约查询json is %@",obj);
             if ([[returnDic valueForKey:@"SUCCESS"]integerValue]==1)
             {
                 NSArray *array = [returnDic valueForKey:@"TT_BOOKING_ORDER"];
                 
                 switch (self.currentItem) {
                     case 0:
                         if (pageIndex==1) {
                             [self.tabelViewLabelArray1 setArray:array];
                         }
                         else{
                             [self.tabelViewLabelArray1 addObjectsFromArray:array];
                         }
                         
                         break;
                         
                     case 1:
                         if (pageIndex==1) {
                             [self.tabelViewLabelArray2 setArray:array];
                         }
                         else{
                             [self.tabelViewLabelArray2 addObjectsFromArray:array];
                         }
                         break;
                         
                     case 2:
                         if (pageIndex==1) {
                             [self.tabelViewLabelArray3 setArray:array];
                         }
                         else{
                             [self.tabelViewLabelArray3 addObjectsFromArray:array];
                         }
                         break;
                     default:
                         break;
                 }
                 
                 [self.detailed1 reloadData];
             }
         }
         else
         {
             NSLog(@"%@",error);
         }
     }];
    
}

-(void)theNewBtn:(id)sender  //新增按钮方法
{
    self.xZengPopViewController = nil;
    self.xZengPopViewController = [[NewPopViewController alloc] init];
    self.xZengPopViewController.delegate = self;
    [self presentView:self.xZengPopViewController.view completion:nil];
    //[self presentPopupViewController:self.xZengPopViewController animationType:MJPopupViewAnimationFade];
}
//返回搜索参数
-(void)getSearchParameters
{
    self.queryDic = [NSMutableDictionary dictionaryWithCapacity:3];
    
    if (![self.thebutton1.titleLabel.text isEqualToString:@"请选择"]) {
        
        [self.queryDic setValue:self.thebutton1.titleLabel.text forKey:@"COMING_DATE_FROM"];
        
        
    }
    if (![self.thebutton2.titleLabel.text isEqualToString:@"请选择"]) {
        
        [self.queryDic setValue:self.thebutton2.titleLabel.text forKey:@"COMING_DATE_TO"];
        
    }
    if (![self.thebutton3.titleLabel.text isEqualToString:@"请选择"]) {
        
        [self.queryDic setValue:self.thebutton3.titleLabel.text forKey:@"COMING_DATE_FROM"];
        
    }
    if (![self.thebutton4.titleLabel.text isEqualToString:@"请选择"]) {
        
        [self.queryDic setValue:self.thebutton4.titleLabel.text forKey:@"COMING_DATE_TO"];
        
    }
    if (![self.saId isEqualToString:@"< 空 >"]) {
        
        [self.queryDic setValue:self.saId forKey:@"SA_NAME_ID"];//TM_EMPLOYEE
        
        
    }
  
    if (![self.inputPersonId isEqualToString:@"< 空 >"]) {
        [self.queryDic setValue:self.inputPersonId forKey:@"REPAIR_TYPE"];//SERVICE_ADVISOR
        
    }
    
    if (self.cardtextfield.text) {
        [self.queryDic setValue:[self.cardtextfield.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"PLATE_NUMBER"];
        
    }
    
    NSInteger arrCount = 0;
    switch (self.currentItem) {
        case 0:
            arrCount = self.tabelViewLabelArray1.count;
            break;
            
        case 1:
            arrCount = self.tabelViewLabelArray2.count;
            break;
            
        case 2:
            arrCount = self.tabelViewLabelArray3.count;
            break;
        default:
            break;
    }

    if (pageIndex==1) {
        [self.queryDic setValue:@"1" forKey:@"FROM"];
        [self.queryDic setValue:@"10" forKey:@"TO"];
    }
    else{
        [self.queryDic setValue:[NSString stringWithFormat:@"%d",arrCount+1] forKey:@"FROM"];
        [self.queryDic setValue:[NSString stringWithFormat:@"%d",arrCount+10] forKey:@"TO"];
    }
    
    
    [self.queryDic setValue:[NSString stringWithFormat:@"%d",self.currentItem+1] forKey:@"TAG"];
}

-(void)loadTableViewCellLabel
{
    CGFloat count=7;
    CGFloat labelW=(self.detailed1.mj_w-count*2)/count;
    CGFloat labelH=40;
    CGFloat margin=2;
    CGFloat marginTop=5;
    self.tableViewlabel2 = [[UILabel alloc]initWithFrame:CGRectMake(margin, marginTop, labelW, labelH)];
    [self.tableViewlabel2 setBackgroundColor:[UIColor clearColor]];

    [self.tableViewlabel2 setTextAlignment:NSTextAlignmentCenter];
    self.tableViewlabel2.font = [UIFont fontWithName:@"ArialMT" size:14];
    self.tableViewlabel2.textColor = [UIColor grayColor];
    
    self.tableViewlabel3 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.tableViewlabel2.frame)+margin, marginTop, labelW, labelH)];
    [self.tableViewlabel3 setBackgroundColor:[UIColor clearColor]];
    [self.tableViewlabel3 setTextAlignment:NSTextAlignmentCenter];
    self.tableViewlabel3.font = [UIFont fontWithName:@"ArialMT" size:14];
    self.tableViewlabel3.textColor = [UIColor grayColor];
    
    self.tableViewlabel4 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.tableViewlabel3.frame)+margin, marginTop, labelW, labelH)];
    [self.tableViewlabel4 setBackgroundColor:[UIColor clearColor]];
    [self.tableViewlabel4 setTextAlignment:NSTextAlignmentCenter];
    self.tableViewlabel4.font = [UIFont fontWithName:@"ArialMT" size:14];
    self.tableViewlabel4.textColor = [UIColor grayColor];
    
    self.tableViewlabel5 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.tableViewlabel4.frame)+margin, marginTop, labelW, labelH)];
    [self.tableViewlabel5 setTextAlignment:NSTextAlignmentCenter];
    [self.tableViewlabel5 setBackgroundColor:[UIColor clearColor]];
    self.tableViewlabel5.font = [UIFont fontWithName:@"ArialMT" size:14];
    self.tableViewlabel5.textColor = [UIColor grayColor];
    

    
    self.tableViewlabel7 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.tableViewlabel5.frame)+margin, marginTop, labelW, labelH)];
    [self.tableViewlabel7 setBackgroundColor:[UIColor clearColor]];

    [self.tableViewlabel7 setTextAlignment:NSTextAlignmentCenter];
    self.tableViewlabel7.font = [UIFont fontWithName:@"ArialMT" size:14];
    self.tableViewlabel7.textColor = [UIColor grayColor];
    
//    self.tableViewlabel8 = [[UILabel alloc]initWithFrame:CGRectMake(545, 5, 100, 40)];
//    [self.tableViewlabel8 setBackgroundColor:[UIColor clearColor]];
//
//    self.tableViewlabel8.font = [UIFont fontWithName:@"ArialMT" size:14];
//    self.tableViewlabel8.textColor = [UIColor grayColor];
//    [self.tableViewlabel8 setTextAlignment:NSTextAlignmentCenter];
//    self.tableViewlabel8.lineBreakMode = NSLineBreakByWordWrapping;
//    self.tableViewlabel8.numberOfLines = 0;
    
    self.tableViewlabel9 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.tableViewlabel7.frame)+margin, marginTop, labelW, labelH)];
    [self.tableViewlabel9 setBackgroundColor:[UIColor clearColor]];
    self.tableViewlabel9.font = [UIFont fontWithName:@"ArialMT" size:14];
    self.tableViewlabel9.textColor = [UIColor grayColor];
    [self.tableViewlabel9 setTextAlignment:NSTextAlignmentCenter];
    self.tableViewlabel9.lineBreakMode = NSLineBreakByWordWrapping;
    self.tableViewlabel9.numberOfLines = 0;
    
    self.tableViewlabel10 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.tableViewlabel9.frame)+margin, marginTop, labelW, labelH)];
    [self.tableViewlabel10 setBackgroundColor:[UIColor clearColor]];
    [self.tableViewlabel10 setTextAlignment:NSTextAlignmentCenter];
    self.tableViewlabel10.font = [UIFont fontWithName:@"ArialMT" size:14];
    self.tableViewlabel10.textColor = [UIColor grayColor];
}
////关闭弹出窗时重新发送消息
-(void)dismissPopupViewControllerWithanimationType:(MJPopupViewAnimation)animationType
{
//    NSLog(@"!?!~?~?~?~~?");
//    [self.secondDetailViewController removeNotification];
//    [[NSNotificationCenter defaultCenter]removeObserver:self.secondDetailViewController name:GETWORKSTATIONLIST object:nil];
//    [[NSNotificationCenter defaultCenter]removeObserver:self.secondDetailViewController.modifyAndAddSubView name:GETWORKSTATIONLIST object:nil];
    [self.secondDetailViewController removeNotification];
    [self.xZengPopViewController removeNotification];
    [super dismissPopupViewControllerWithanimationType:animationType];
}

-(void)addNotification
{
    //注册消息
    //查询
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData:) name:SEARCHPRECONTRACTMANAGE object:Nil];
    //服务顾问
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSaName:) name:GETSANAMELIST object:Nil];
    //预约登记人
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getInputPersonName:) name:GETINPUTPERSONLIST object:Nil];
    //预约详情
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getprecontractDetail:) name:GETPRECONTRACTDETAIL object:Nil];
    
    //工位
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getworkStationList:) name:GETWORKSTATIONLIST object:Nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(xugaisucess) name:@"sucess" object:Nil];
}
-(void)removeNotification
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}

#pragma mark - NewPopViewDelegate
- (void)addNewSuccess
{
    pageIndex = 1;
    [self chare:nil];
    
}


@end
